package com.wp.User.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.wp.User.pojo.User;
import com.wp.User.service.UserService;

@Controller
public class UserController{
	
	@Autowired
	private UserService userService;
	
	@RequestMapping("/login.do")
	@ResponseBody
	public User login(User user) {
		User u = userService.login(user);
		if(u != null) {
			userService.updateTime(u);
		}
		return u;
	}
	
	@RequestMapping("/userList.do")
	@ResponseBody
	public List<User> selectAll(){
		List<User> list = userService.selectAll();
		return list;
	}
	
	@RequestMapping("/deleteById.do")
	@ResponseBody
	public boolean deleteUser(User user) {
		boolean result = userService.deleteUserById(user);
		return result;
	}

	@RequestMapping("/addUser.do")
	public String addUser(@RequestParam("img") MultipartFile f1,User user) {
		String filename = f1.getOriginalFilename();
		File file = new File("D:\\apache-tomcat-8.0.33\\wtpwebapps\\ordersystem\\images",filename);
		try {
			f1.transferTo(file);
		}catch(IllegalStateException e){
			e.printStackTrace();
		}catch(IOException e) {
			e.printStackTrace();
		}
		user.setUimage("images"+filename);
		boolean result = userService.addUser(user);
		System.out.println("添加用户结果为：" + result);
		return "userList";
	}
	
	@RequestMapping("/updateUserPage.do")
	public String updatePage(User user,HttpSession session) {
		session.setAttribute("uid",user.getUid());
		return "updateUser";
	}
	@RequestMapping("/selectUser.do")
	@ResponseBody
	public User selectUser(HttpSession session) {
		User u = (User) session.getAttribute("uid");
		User user = userService.selectUserById(u);
		return user;
	}
	
	@RequestMapping("/selectUserWithUsername.do")
	@ResponseBody
	public List<User> selectUserWithUsername(User u){
		System.out.println("接收的数据为：" + u);
		List<User> list = userService.selectUserWithUsername(u);
		return list;
	}
}
